#!/usr/bin/python
# -*- coding: utf-8 -*-

import datetime

from django.db import models
from django.contrib.auth.models import User


class Author(models.Model):
    name = models.CharField(max_length=45, verbose_name=u'imię/nazwa')
    surname = models.CharField(max_length=45, blank=True,
                               verbose_name='nazwisko')
    birth = models.DateField(blank=True, null=True,
                             verbose_name=u'data urodzin')

    def __unicode__(self):
        text = self.name
        text = text + ' ' + self.surname if self.surname else text
        return text

    class Meta:
        verbose_name = 'autor'
        verbose_name_plural = 'autorzy'


class Publisher(models.Model):
    name = models.CharField(max_length=45, verbose_name=u'nazwa', unique=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'wydawca'
        verbose_name_plural = 'wydawcy'


class Media(models.Model):
    MEDIAS = (
             ('CD', 'CD'),
             ('DV', 'DVD'),
             ('DI', 'Cyfrowy'),
             ('PA', 'Papier'),
             ('VI', 'Winyl'),
    )
    name = models.CharField(max_length=2, choices=MEDIAS, unique=True,
                            verbose_name='nazwa')

    def __unicode__(self):
        return self.get_name_display()

    def as_icon(self):
        from IPython import embed
        icon = '<i class="icon-%s" title="%s"></i>'
        icon_name = {
            'PA': ['book', ],
            'CD': ['headphones', ],
            'VI': ['music', ],
            'DV': ['film', ],
            'DI': ['download-alt', ]
        }
        return icon % (icon_name[self.name][0], self.get_name_display())

    class Meta:
        verbose_name = u'nośnik'
        verbose_name_plural = u'nośniki'


class LibraryUser(models.Model):
    user = models.OneToOneField(User, verbose_name='użytkownik')

    def __unicode__(self):
        return self.user.first_name + ' ' + self.user.last_name + ' (' +\
            self.user.username + ')'

    class Meta:
        verbose_name = u'czytelnik'
        verbose_name_plural = u'czytelnicy'


class LibraryStaff(LibraryUser):

    class Meta:
        verbose_name = u'bibliotekarz'
        verbose_name_plural = u'bibliotekarze'
        permissions = (
            ('LibraryStaff', u'Może edytować i usuwać zamówienia'),
        )


class Item(models.Model):
    title = models.CharField(max_length=45, verbose_name=u'tytuł')
    date = models.DateField(verbose_name='rok wydania')
    author = models.ForeignKey(Author, verbose_name='autor')
    media = models.ForeignKey(Media, verbose_name='media')
    publisher = models.ForeignKey(Publisher, verbose_name='wydawca')
    isbn = models.DecimalField(max_digits=13, decimal_places=0, blank=True,
        null=True, unique=True, verbose_name='ISBN')
    url = models.URLField(blank=True, null=True, verbose_name='URL')
    is_available = models.BooleanField(default=True, verbose_name=u'dostępne')
    is_digital = models.BooleanField(default=False, verbose_name=u'cyfrowe')

    def __unicode__(self):
        return self.title + ' (' + unicode(self.date.year) + ')'

    class Meta:
        verbose_name = 'pozycja'
        verbose_name_plural = 'pozycje'


class History(models.Model):
    library_user = models.ForeignKey(LibraryUser, verbose_name=u'użytkownik')
    item = models.ForeignKey(Item, verbose_name='pozycja')
    due_date = models.DateField(verbose_name=u'zwrot')

    class Meta:
        verbose_name = 'historia'
        verbose_name_plural = 'historie'


def until_date():
    return datetime.date.today() + datetime.timedelta(days=14)


class Order(models.Model):
    library_user = models.ForeignKey(LibraryUser, verbose_name=u'użytkownik')
    item = models.ForeignKey(Item, verbose_name=u'pozycja')
    due_date = models.DateField(default=until_date, verbose_name=u'zwrot')
    is_ready = models.BooleanField(default=False, verbose_name=u'do odbioru')

    def delete(self, *args, **kwargs):
        History.objects.create(library_user=self.library_user, item=self.item,
            due_date=self.due_date)
        super(Order, self).delete(*args, **kwargs)

    def __unicode__(self):
        return self.item.title + u', użytkownik ' + unicode(self.library_user)

    class Meta:
        verbose_name = u'zamówienie'
        verbose_name_plural = u'zamówienia'
